import sys
import random

# Put this file in the same folder as the csv for cartoon and non cartoon
cart_filename = 'iscartoon.csv'
real_filename = 'noncartoon.csv'
if len(sys.argv) < 3:
	print "Correct syntax: extract.py [num-cartoon] [num-noncartoon] [filename]"
	sys.exit()

# read arguments
ncart = int(sys.argv[1])
nreal = int(sys.argv[2])
out_filename = "merged/MERGED_"+sys.argv[3]

# open input and output file
cart_file = open(cart_filename, 'r')
real_file = open(real_filename, 'r')
outfile = open(out_filename, 'w')

# write to output
cart_lines = cart_file.readlines()
real_lines = real_file.readlines()

# Extraction parameters is larger than input
if ncart>len(cart_lines) or nreal>len(real_lines):
	sys.exit("ERROR: Sample larger than population")
outfile.write("".join(random.sample(cart_lines, ncart)))
outfile.write("".join(random.sample(real_lines, nreal)))
print "Read     :", len(cart_lines), "cartoons,", len(real_lines), "reals."
print "Acquired :", ncart, "cartoons,", nreal, "reals."
print "Output   :", out_filename.upper()
